// @author U095159L VARUN GANESH
// all functions in this file  are implemented by the author unless specifically indicated
var FightSystem = function () {
	var scoreTable;
	var history = new Array(3,3,3);
	var transitions;
	var gainTable;

	this.init = function(score_table){
		scoreTable = score_table;
		initTransitionTable();
		initGainTable(scoreTable);
	}


	//Init all transition table entries to 1
	function initTransitionTable(){
		var x = parseInt(1);
		transitions = new Array(4);
		for( var i = 0; i < 4; ++i){
			transitions[i] = new Array(4);
			for(var j = 0; j < 4; ++j){
				transitions[i][j] = [x,x,x,x];
			}
		}
	}

	//Init gain table with difference of opponent to player score
	function initGainTable(scoreTable){
		gainTable = new Array(4);
		for( var i = 0; i < 4; ++i){
			gainTable[i] = new Array(4);
			for(var j = 0; j < 4; ++j){
				gainTable[i][j] = scoreTable[j][i] - scoreTable[i][j];
			}
		}
	}

	this.update = function(move){
		//console.log("Update", FIGHT_MODE);
		history = [history[1],history[2],move];
		var x = transitions[history[0]][history[1]][history[2]];
		var y = parseInt(x);
		var z =  y + 1;
		transitions[history[0]][history[1]][history[2]] = z;
	};

	function predict(){
		// probability distribution over next move
		var prob = new Array(4);
		
		// look up previous transitions from moves at time t-2, t-1
		var tr = transitions[history[1]][history[2]];
		
		// normalizing constant
		var sum = 0;
		for (var k = 0; k < 4; k++) {
			sum += tr[k];
		}
		
		for (k = 0; k < 4; k++) {
			prob[k] = tr[k] / sum;
		}
		
		return prob;			
	}
	

	this.chooseMove = function(){
			// predict player's move
			var prob = predict();
			
			// expected score table
			var score = new Array(4);
			for (var i = 0; i < 4; i++) {
				for (var j = 0; j < 4; j++) {
					if (i == 0){
						score[j] = 0;
					}
					score[j] += prob[i] * gainTable[i][j];
				}
			}
			
			// find move with maximal expected score
			var bestScore = -Number.MIN_VALUE;
			var bestMove = 0;
			for (i = 0; i < 4; i++) {
				if (score[i] > bestScore) {
					bestScore = score[i];
					bestMove = i;
				}
			}
			
			//trace(gainTable);
			//trace('p', prob);
			//trace('s', score, bestMove);
			return bestMove;
		};

}  
var normalFightSystem = new FightSystem();
var hardFightSystem = new FightSystem();